package cn.tedu.management.portal.service.impl;

import cn.tedu.management.portal.dto.DepartmentDTO;
import cn.tedu.management.portal.dto.SelectDeptDTO;
import cn.tedu.management.portal.ex.InsertException;
import cn.tedu.management.portal.ex.NotAManagerException;
import cn.tedu.management.portal.ex.NotFoundException;
import cn.tedu.management.portal.ex.UpdateException;
import cn.tedu.management.portal.mapper.StaffMapper;
import cn.tedu.management.portal.model.Department;
import cn.tedu.management.portal.mapper.DepartmentMapper;
import cn.tedu.management.portal.model.Staff;
import cn.tedu.management.portal.service.IDepartmentService;
import cn.tedu.management.portal.vo.DepartmentVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    StaffMapper staffMapper;

    @Override
    public void addDepartment(DepartmentDTO departmentDTO) {
        //判断用户是否存在
        String phone=departmentDTO.getPhone();
        Staff staff=staffMapper.findByPhone(phone);
        if (staff == null){
            //负责人不存在
            throw new NotFoundException("该负责人不存在!");
        }
        //如果用户存在,是否为部门负责人
        Integer jobId=staff.getJobId();
        if (jobId != 3){
            throw new NotAManagerException("该员工不是一位部门负责人!");
        }
        LocalDateTime now=LocalDateTime.now();
        Department department=new Department();
        department.setDepartment(departmentDTO.getDepartment());
        department.setPId(departmentDTO.getPId());
        department.setState(departmentDTO.getState());
        department.setManager(departmentDTO.getManager());
        department.setPhone(departmentDTO.getPhone());
        department.setEmail(departmentDTO.getEmail());
        department.setGmtCreate(now);
        department.setGmtModified(now);

        int rows=departmentMapper.insert(department);
        if (rows != 1){
            throw new InsertException("服务器忙!请稍后重试!");
        }

    }

    @Override
    public DepartmentVO showDeptById(Integer id) {
        return departmentMapper.showUpdateDeptById(id);
    }

    @Override
    public void updateDepartment(DepartmentDTO departmentDTO) {
        Integer id=departmentDTO.getId();
        Department department=departmentMapper.selectById(id);
        if (department == null){
            throw new NotFoundException("该部门不存在!");
        }
        department.setPId(departmentDTO.getPId());
        department.setDepartment(departmentDTO.getDepartment());
        department.setState(departmentDTO.getState());
        department.setManager(departmentDTO.getManager());
        department.setPhone(departmentDTO.getPhone());
        department.setEmail(departmentDTO.getEmail());

        LocalDateTime now=LocalDateTime.now();
        department.setGmtModified(now);

        int rows=departmentMapper.updateById(department);
        if (rows != 1){
            throw new UpdateException("修改失败!服务器忙,请稍后重试!");
        }
    }

    @Override
    public List<Department> showDepartments() {
        return departmentMapper.findAll();
    }

    @Override
    public void deleteDeptById(Integer id) {
        Department department=departmentMapper.selectById(id);
        if (department == null){
            throw new NotFoundException("该用户不存在!");
        }
        departmentMapper.deleteById(id);
    }

    @Override
    public Department findADept(SelectDeptDTO selectDeptDTO) {
        String name=selectDeptDTO.getDepartment();
        Integer state=selectDeptDTO.getState();
        Department department=departmentMapper.findByNameAndState(name,state);
        return department;
    }
}
